package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.seller.TbItemCat;
import com.dongdongshop.pojo.seller.TbTypeTemplate;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.TemplService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;


import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("itemCatController")
public class ItemCatController {

    private Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Reference
    private ItemCatService itemCatService;

    @Reference(timeout = 1000000, check = false)
    private TemplService templService;

    @RequestMapping("toList")
    public String toList(Model model){
        List<TbTypeTemplate> templates =templService.findInfoAll();
        model.addAttribute("templates",templates);
        return "admin/item_cat";
    }

        @RequestMapping("getInfoByPid")
        @ResponseBody
        public Result getInfoByPid(Long parentId){
            try {
                List<TbItemCat> list = itemCatService.getInfoByPid(parentId);
                logger.info("查询成功");
                return Result.response(ResultEnum.SUCCESS).setData(list);
            }catch (Exception e){
                logger.error(ResultEnum.ERROR.getMessage());
                return Result.response(ResultEnum.ERROR);
            }
    }

    @RequestMapping("addInfo")
    @ResponseBody
    public Result addInfo(TbItemCat itemCat){
        try {
            itemCatService.addInfo(itemCat);
            logger.info("增加成功");
            return Result.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage());
            return Result.response(ResultEnum.ERROR);
        }
    }


    //批量删除
    @RequestMapping("deleteByIds")
    @ResponseBody
    public Result deleteByIds(@RequestParam("ids[]")Integer[] ids){

        try {
            int i = itemCatService.deleteInfo(ids);
            return Result.response(ResultEnum.SUCCESS).setData(i);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.response(ResultEnum.ERROR);
        }
    }
    //回显
    @RequestMapping("eachItemInfo")
    @ResponseBody
    public Result eachItemInfo(Long id){

        try {
            TbItemCat itemCat = itemCatService.eachItemInfo(id);
            return Result.response(ResultEnum.SUCCESS).setData(itemCat);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.response(ResultEnum.ERROR);
        }
    }

    //修改
    @RequestMapping("updateItemInfo")
    @ResponseBody
    public Result updateItemInfo(TbItemCat itemCat){
        try {
            itemCatService.updateItemInfo(itemCat);
            return Result.response(ResultEnum.SUCCESS).setData(itemCat);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.response(ResultEnum.ERROR);
        }
    }
}
